package com.resourcestore.action;
import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.CourseFileSrcPrivateDao;
import com.resourcestore.model.CourseFileSrcPrivate;
import com.resourcestore.model.CourseFileSrcPublic;

public class TeacherCourseFileSrcPrivateAction extends BaseAction {
	
	private CourseFileSrcPrivate courseFileSrcPrivate;
	private List<CourseFileSrcPrivate> listCourseFileSrcPrivate;
	private CourseFileSrcPrivateDao courseFileSrcPrivateDao = new CourseFileSrcPrivateDao();
	private Pagination pagination = new Pagination();

	
	/**
	 * 查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String listCourseFileSrcPrivate(){
		if(!CommonUtil.isNotNullOrNotEqualsEmpty(courseFileSrcPrivate)){
			courseFileSrcPrivate = new CourseFileSrcPrivate();
			courseFileSrcPrivate.setCourseFileCategoryId(-2);
		}
		if(courseFileSrcPrivate.getCourseFileCategoryId()==0){
			courseFileSrcPrivate.setCourseFileCategoryId(-2);
		}
		pagination.setTotal(courseFileSrcPrivateDao.queryCourseFileSrcPrivateNum(courseFileSrcPrivate));
		listCourseFileSrcPrivate = courseFileSrcPrivateDao.selectCourseFileSrcPrivatePage(courseFileSrcPrivate,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcPrivate.size(); i++) {
//			if(CommonUtil.isNotNullOrNotEqualsEmpty(listCourseFileSrcPrivate.get(i).getCourseName())){
//				listCourseFileSrcPrivate.get(i).setCourseName(listCourseFileSrcPrivate.get(i).getCourseName()+"("+listCourseFileSrcPrivate.get(i).getRealName()+")");
//			}
			//将文件类型代码转换为名称
			listCourseFileSrcPrivate.get(i).setCourseFileTypeName(this.showFileCate(listCourseFileSrcPrivate.get(i).getCourseFileType()));	
			try {
					listCourseFileSrcPrivate.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPrivate.get(i).getGmtCreated()));
					listCourseFileSrcPrivate.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPrivate.get(i).getGmtModified()));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		}
		
		return "default";
	}
	
	
	/**
	 * 根据条件搜索，查询返回List带分页
	 * @throws ParseException 
	 * 
	 */
	public String searchListCourseFileSrcPrivate(){
		if(courseFileSrcPrivate.getCourseFileCategoryId()==0){
			courseFileSrcPrivate.setCourseFileCategoryId(-2);
		}
		//对内容进行urldecoder解码
		try {
			courseFileSrcPrivate.setCourseFileName(new String(courseFileSrcPrivate.getCourseFileName().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcPrivate.setCourseFileRemark(new String(courseFileSrcPrivate.getCourseFileRemark().getBytes("iso-8859-1"),"UTF-8"));
			courseFileSrcPrivate.setCourseName(new String(courseFileSrcPrivate.getCourseName().getBytes("iso-8859-1"),"UTF-8"));
			//courseFileSrcPrivate.setAuthor(new String(courseFileSrcPrivate.getAuthor().getBytes("iso-8859-1"),"UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		pagination.setTotal(courseFileSrcPrivateDao.queryCourseFileSrcPrivateNum(courseFileSrcPrivate));
		listCourseFileSrcPrivate = courseFileSrcPrivateDao.selectCourseFileSrcPrivatePage(courseFileSrcPrivate,pagination);
		
		//将bigint数字处理为时间格式
		for (int i = 0; i < listCourseFileSrcPrivate.size(); i++) {
			try {
			listCourseFileSrcPrivate.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPrivate.get(i).getGmtCreated()));
			listCourseFileSrcPrivate.get(i).setGmtModifiedString(CommonUtil.getSysTimeFormatString(listCourseFileSrcPrivate.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return newpath("/TeacherCourseFileSrcPrivate/listCourseFileSrcPrivate.jsp");
	}
	
	/**
	 * 预插入一条数据
	  */		
	public String preInsertCourseFileSrcPrivate(){
		
		return newpath("/TeacherCourseFileSrcPrivate/insertCourseFileSrcPrivate.jsp");		
	}
	
	/**
	 * 插入一条数据
	  */		
	public String insertCourseFileSrcPrivate(){
		
		try {
			courseFileSrcPrivate.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcPrivate.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcPrivateDao.insertCourseFileSrcPrivate(courseFileSrcPrivate);
		return redirect(this.getUrl());		
	}
	
	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateCourseFileSrcPrivate(){
		courseFileSrcPrivate = courseFileSrcPrivateDao.selectCourseFileSrcPrivateById(courseFileSrcPrivate);
		
		return newpath("/TeacherCourseFileSrcPrivate/updateCourseFileSrcPrivate.jsp");
	}
	/**
	 * 更新信息
	 * 
	 */
	public String updateCourseFileSrcPrivate(){
		
		try {
			//courseFileSrcPrivate.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			courseFileSrcPrivate.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		courseFileSrcPrivateDao.updateCourseFileSrcPrivate(courseFileSrcPrivate);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteCourseFileSrcPrivate(){
		
		//删除服务器文件
		CourseFileSrcPrivate courseFileSrcPrivateTemp = new CourseFileSrcPrivate();
		courseFileSrcPrivateTemp = courseFileSrcPrivateDao.selectCourseFileSrcPrivateById(courseFileSrcPrivate);
		String downloadUrl = courseFileSrcPrivateTemp.getDownloadUrl();
		int numFlag = downloadUrl.indexOf("upload/");
		if(numFlag!=-1){
			String fileName = downloadUrl.substring(numFlag).substring(7);
			CommonUtil.deleteFile(ServletActionContext.getServletContext()
					.getRealPath("upload")
					+ "/" + fileName);
		}
		
		courseFileSrcPrivateDao.deleteCourseFileSrcPrivate(courseFileSrcPrivate);
		return redirect(this.getUrl());
	}
	
	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseFileSrcPrivate(){
		
		//删除服务器文件
		List<CourseFileSrcPrivate> listTemp = courseFileSrcPrivateDao.selectCourseFileSrcPrivateByCourseFileIdString(courseFileSrcPrivate);
		for(int i=0;i<listTemp.size();i++){
			if(CommonUtil.isNotNullOrNotEqualsEmpty(listTemp.get(i).getDownloadUrl())){
				
				String downloadUrl = listTemp.get(i).getDownloadUrl();
				int numFlag = downloadUrl.indexOf("upload/");
				if(numFlag!=-1){
					String fileName = downloadUrl.substring(numFlag).substring(7);
					CommonUtil.deleteFile(ServletActionContext.getServletContext()
							.getRealPath("upload")
							+ "/" + fileName);
				}
			}
		}
		
		courseFileSrcPrivateDao.deleteMoreCourseFileSrcPrivate(courseFileSrcPrivate);
		return redirect(this.getUrl());
	}

	
	public CourseFileSrcPrivate getCourseFileSrcPrivate() {
		return courseFileSrcPrivate;
	}

	public void setCourseFileSrcPrivate(CourseFileSrcPrivate courseFileSrcPrivate) {
		this.courseFileSrcPrivate = courseFileSrcPrivate;
	}

	public List<CourseFileSrcPrivate> getListCourseFileSrcPrivate() {
		return listCourseFileSrcPrivate;
	}

	public void setListCourseFileSrcPrivate(List<CourseFileSrcPrivate> listCourseFileSrcPrivate) {
		this.listCourseFileSrcPrivate = listCourseFileSrcPrivate;
	}
	
	 public Pagination getPagination() {
		return this.pagination;
	 }

	 public void setPagination(Pagination pagination) {
		 this.pagination = pagination;
	 }
}
